package com.ficus.road.maintain.third.zh.roadgps;

import com.ficus.road.maintain.core.model.GpsLocation;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.ficus.road.maintain.util.ZHTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author xiawen yang
 * @date 2022/4/1 下午4:35
 */
@Component
@Slf4j
public class ZHRoadName extends BaseZH {

    // 子类不声明这个静态变量，下面的赋值语句会修改父类的静态变量的值
    private Integer connectTimeout;
    @Autowired
    private ZHTokenUtil zhTokenUtil;
    @Value("${zhonghai.ip}")
    private String ip;
    @Value("${zhonghai.webServicePort}")
    private int webServicePort;
    @Value("${zhonghai.username}")
    private String username;

    @Override
    public Integer getConnectTimeout() {
        return this.connectTimeout;
    }

    @PostConstruct
    void init() {
        arg0 = "ytkjByDingXyRoad";
        errMsg = "调用中海接口获取道路名称异常!";
        successMsg = "调用中海接口获取道路名称成功!";
        connectTimeout = 3000;
    }

    public synchronized String getRoadName(GpsLocation gpsLocation) {
        arg1 = "{\"x\":\"" + gpsLocation.getLongitude() + "\",\"y\":\"" + gpsLocation.getLatitude() + "\"}";
        String roadNameStr;
        try {
            roadNameStr = getZHData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (ObjectUtils.isEmpty(roadNameStr) || roadNameStr.length() <= 13) {
            return null;
        }
        roadNameStr = roadNameStr.substring(10, roadNameStr.length() - 3);
        return roadNameStr;
    }
}
